//---------------------------------------------------------------------
//  This file is part of the Background Motion solution.
// 
//  Copyright (C) Mindscape (TM).  All rights reserved.
//  http://www.mindscape.co.nz
// 
//  THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//---------------------------------------------------------------------

using Microsoft.Practices.EnterpriseLibrary.Validation;

using Mindscape.BackgroundMotion.Core.Validators;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace Mindscape.BackgroundMotion.Model
{
  /// <summary>
  /// Represents a type of content which can exist in the system (e.g. Video or Image)
  /// </summary>
  [Table(Name = "ContentType")]
  public sealed class ContentType : ModelObject
  {
    private int _id;
    private string _description;

    private readonly EntitySet<Contribution> _contributions;

    public ContentType()
    {
      _contributions = CreateEntitySet<Contribution>(
        (Contribution c) => c.ContentType = null,
        (Contribution c) => c.ContentType = this
      );
    }

    /// <summary>
    /// Validates this instance for a correct state
    /// </summary>
    /// <remarks>
    /// This is called by the Enterprise Library Validation framework
    /// </remarks>
    /// <param name="results">A set of ValidationResults indicating validation issues with the current instance</param>
    protected override void Validate(ValidationResults results)
    {
      results.AddAllResults(Validation.ValidateFromAttributes(this));
    }

    // Constants
    public const int Video = 1;
    public const int Image = 2;

    #region Mapped Properties

    /// <summary>
    /// The unique identifier of this instance
    /// </summary>
    [Column(Storage = "_id", IsPrimaryKey = true, IsDbGenerated = true)]
    public override int Id
    {
      get { return _id; }
      set
      {
        if (_id != value)
        {
          OnPropertyChanging("Id");
          _id = value;
          OnPropertyChanged("Id");
        }
      }
    }

    /// <summary>
    /// The description of this instance
    /// </summary>
    [RequireValidator("Description", 50)]
    [Column(Storage = "_description")]
    public string Description
    {
      get { return _description; }
      set
      {
        if (_description != value)
        {
          OnPropertyChanging("Description");
          _description = value;
          OnPropertyChanged("Description");
        }
      }
    }

    /// <summary>
    /// The set of Contributions which are associated with this ContentType
    /// </summary>
    [Association(Storage = "_contributions", OtherKey = "ContentTypeId")]
    public EntitySet<Contribution> Contributions
    {
      get { return _contributions; }
      set { _contributions.Assign(value); }
    }

    #endregion
  }
}